using UnityEngine;
using UnityEditor;
using UnityEditor.IMGUI.Controls;

namespace Hotfix
{
    /// <summary>
    /// 
    /// </summary>
    public class XHotfixAssetTreeViewItem : TreeViewItem
    {
        /// <summary>
        /// 
        /// </summary>
        private XHotfixAssetBundleAssetInfo assetInfo;

        /// <summary>
        /// 
        /// </summary>
        public XHotfixAssetBundleAssetInfo asset
        {
            get { return assetInfo; }
        }

        /// <summary>
        /// 
        /// </summary>
        public XHotfixAssetTreeViewItem()
            : base(-1, -1)
        {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="a"></param>
        public XHotfixAssetTreeViewItem(XHotfixAssetBundleAssetInfo a)
            : base(a != null ? a.fullAssetName.GetHashCode() : Random.Range(int.MinValue, int.MaxValue), 0, a != null ? a.displayName : "failed")
        {
            assetInfo = a;
            if (a != null)
                icon = AssetDatabase.GetCachedIcon(a.fullAssetName) as Texture2D;
        }

        /// <summary>
        /// 
        /// </summary>
        private Color color = new Color(0, 0, 0, 0);

        /// <summary>
        /// 
        /// </summary>
        public Color itemColor
        {
            get
            {
                if (color.a == 0.0f && assetInfo != null)
                {
                    color = assetInfo.GetColor();
                }

                return color;
            }

            set { color = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public Texture2D MessageIcon()
        {
            return XHotfixMessageSystem.GetIcon(HighestMessageLevel());
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public MessageType HighestMessageLevel()
        {
            return assetInfo != null ? assetInfo.HighestMessageLevel() : MessageType.Error;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="asset"></param>
        /// <returns></returns>
        public bool ContainsChild(XHotfixAssetBundleAssetInfo asset)
        {
            bool contains = false;
            if (children == null)
                return contains;

            if (asset == null)
                return false;

            foreach (var child in children)
            {
                var c = child as XHotfixAssetTreeViewItem;
                if (c != null && c.asset != null && c.asset.fullAssetName == asset.fullAssetName)
                {
                    contains = true;
                    break;
                }
            }

            return contains;
        }
    }

}